<?php
class User_account_model extends BZ_Model
{
    protected $table = 'user_account';

    public function findByUid($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }

    public function updateAmountCarry($uid, $amount_carry)
    {
        $this->db->set('amount_carry', 'amount_carry-'.$amount_carry, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }
    
    /**
     * 验证金币是否可以支付
     * @param unknown $uid
     * @param unknown $amount_bonus
     */
    public function validataUserAccountUb($uid, $amount_bonus)
    {
        $this->db->where('`amount_bonus`>', $amount_bonus, false);
        $this->db->where('uid', $uid);
        return $this->db->count_all_results($this->table);
    }

    /**
     * 下订单 扣除金币
     * @param unknown $uid
     * @param unknown $amount_bonus
     */
    public function updateUserAccountUb($uid, $amount_bonus)
    {
        $this->db->set('amount_bonus', 'amount_bonus-'.$amount_bonus, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 取消订单和过期订单 退回金币
     * @param unknown $uid
     * @param unknown $amount_bonus
     */
    public function returnUserAccountUb($uid, $amount_bonus)
    {
        $this->db->set('amount_bonus', 'amount_bonus+'.$amount_bonus, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 订单分润
     * @param unknown $orderProfit
     */
    public function userAccountProfit($orderProfit)
    {
        foreach ($orderProfit as $item) {
            if ($item->uid <= 0 || $item->account <= 0) {
                continue;
            }
            $result = $this->findByUid($item->uid);
            if ($result->num_rows() <= 0) {
                $error[] = '此账户UID('.$item->uid.')不存在';
            }
            $userAccount = $result->first_row();
            $account_name = getAccountNameByAccountID($item->account_type); //可提现或月结 （字段）
//             if ($item->account_type == ACCOUNT_TYPE_MONTH && (int)$item->uid&UTID_PROVIDER) { //如果是到付，并且是供应商帐号，欠我们钱。
//                 $item->amount = -$item->amount;
//             }
            if ($item->as == 2) { //减
                $item->amount = -$item->account;
                $total_name   = 'outlay_total';  //总支出
                $total_amount = $item->account;
            } else {
                $total_name   = 'income_total';  //总收入
                $total_amount = $item->account;
            }
            //执行分润，保存到表
            $this->db->set($account_name, "$account_name+".$item->account, FALSE);
            $this->db->set($total_name, "$total_name+".$total_amount, FALSE);
            $this->db->where('uid', $item->uid);
            $this->db->limit(1);
            $this->db->update($this->table);
            if ($item->account_type == ACCOUNT_TYPE_MONTH) { //到付
                $type = DFPROFIT;
            } else { //在线利润
                $type = ZFPROFIT;
            }
            $this->account_log->insertAccountLogProfit($item, $userAccount->amount_carry, $type);
        }
    }

    /**
     * 订单划款 供应商账户获得金额
     * @param $uid
     * @param $amount_carry
     * @return mixed
     */
    public function updateUserAccountAdvance($uid, $amount_carry)
    {
        $this->db->set('amount_carry', 'amount_carry+'.$amount_carry, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 单笔分润操作
     * @param $account_name
     * @param $total_name
     * @param $totalAmount
     * @param $item
     * @return mixed
     */
    public function updateUserAccountProfit($account_name, $total_name, $totalAmount, $item)
    {
        $this->db->set($account_name, "$account_name+".$item->account, FALSE);
        $this->db->set($total_name, "$total_name+".$totalAmount, FALSE);
        $this->db->where('uid', $item->uid);
        $this->db->limit(1);
        return $this->db->update($this->table);
    }


    /**
     * 运费 供应商账户获得金额
     * @param $uid
     * @param $amount_carry
     * @return mixed
     */
    public function updateUserFreight($uid, $amount_carry)
    {
        $this->db->set('amount_carry', 'amount_carry+'.$amount_carry, false);
        $this->db->set('income_total', 'income_total+'.$amount_carry, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 用户添加
     * @param $uid
     * @param int $amount_bonus
     * @return mixed
     */
    public function insertUserAccount($uid, $amount_bonus = 0)
    {
        $data = array(
            'uid'          => $uid,
            'over_beizhu'  => 0,
            'poundage'     => 0,
            'bargaining'   => 0,
            'amount_carry' => 0,
            'amount_settlement' => 0,
            'amount_bonus' => $amount_bonus,
        );
        // if ($parent_id != UTID_BEIZHU) {
        //     $data['amount_bonus'] = 300;
        // }

        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 用户提现  将体现金额  转到冻结金额中
     * @param $uid
     * @param $amount
     * @return mixed
     */
    public function userWithdraw($uid, $amount)
    {
        $this->db->set('amount_carry', 'amount_carry-'.$amount, false);
        $this->db->set('amount_settlement', 'amount_settlement+'.$amount, false);
        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 用户充值 提现现金增加(amount_carry) 用户总收入增加(income_total)
     * @param $uid
     * @param $amount
     * @return mixed
     */
    public function userDeposit($uid, $amount)
    {
        $this->db->set('amount_carry', 'amount_carry+'.$amount, false);
        $this->db->set('income_total', 'income_total+'.$amount, false);
        $this->db->where('uid', $uid);
        $this->db->limit(1);
        return $this->db->update($this->table);
    }
    
    public function findByUserAccount($uid)
    {
        $this->db->select('over_beizhu, poundage, bargaining, amount_carry, amount_bonus, amount_month');
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }

    /**
     * 修改账户资金
     * @param $uid
     * @param array $account
     * @return mixed
     */
    public function updateUserAccount($uid, $account=array())
    {
        $data = array();
        if (isset($account['amount_month'])) {
            $this->db->set('amount_month', 'amount_month-'.$account['amount_month'], FALSE);
        }
        if (isset($account['amount_carry'])) {
            $this->db->set('amount_carry', 'amount_carry+'.$account['amount_carry'], FALSE);
        }
        if (isset($account['amount_bonus'])) {
            $this->db->set('amount_bonus', 'amount_bonus+'.$account['amount_bonus'], FALSE);
        }
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }

    public function insertVoucherUserAccount($postData, $uid)
    {
        $data = array(
            'uid'          => $uid,
            'over_beizhu'  => 0,
            'poundage'     => 0,
            'bargaining'   => 0,
            'amount_carry' => 0,
            'amount_settlement' => 0,
            'amount_bonus' => 0,
        );
        if (!empty($postData['amount_carry'])) {
            $data['amount_carry'] = $postData['amount_carry'];
        }
        if (!empty($postData['over_beizhu'])) {
            $data['over_beizhu'] = $postData['over_beizhu'];
        }
        if (!empty($postData['amount_bonus'])) {
            $data['amount_bonus'] = $postData['amount_bonus'];
        }

        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function updateVoucherUserAccount($uid, $voucher_type, $amount)
    {
        if (1==$voucher_type) {
            $this->db->set('amount_carry',"amount_carry + $amount",false);
        } elseif (2==$voucher_type) {
            $this->db->set('amount_bonus',"amount_bonus + $amount",false);
        }

        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }
}
